package cn.iocoder.yudao.module.service.controller.admin.welcomeconfig;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.service.controller.admin.welcomeconfig.vo.ServiceWelcomeConfigPageReqVO;
import cn.iocoder.yudao.module.service.controller.admin.welcomeconfig.vo.ServiceWelcomeConfigRespVO;
import cn.iocoder.yudao.module.service.controller.admin.welcomeconfig.vo.ServiceWelcomeConfigSaveReqVO;
import cn.iocoder.yudao.module.service.dal.dataobject.welcomeconfig.ServiceWelcomeConfigDO;
import cn.iocoder.yudao.module.service.service.welcomeconfig.ServiceWelcomeConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 欢迎语配置")
@RestController
@RequestMapping("/service/welcome-config")
@Validated
public class ServiceWelcomeConfigController {

    @Resource
    private ServiceWelcomeConfigService welcomeConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建欢迎语配置")
    @PreAuthorize("@ss.hasPermission('service:welcome-config:create')")
    public CommonResult<Long> createWelcomeConfig(@Valid @RequestBody ServiceWelcomeConfigSaveReqVO createReqVO) {
        return success(welcomeConfigService.createWelcomeConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新欢迎语配置")
    @PreAuthorize("@ss.hasPermission('service:welcome-config:update')")
    public CommonResult<Boolean> updateWelcomeConfig(@Valid @RequestBody ServiceWelcomeConfigSaveReqVO updateReqVO) {
        welcomeConfigService.updateWelcomeConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除欢迎语配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('service:welcome-config:delete')")
    public CommonResult<Boolean> deleteWelcomeConfig(@RequestParam("id") Long id) {
        welcomeConfigService.deleteWelcomeConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得欢迎语配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('service:welcome-config:query')")
    public CommonResult<ServiceWelcomeConfigRespVO> getWelcomeConfig(@RequestParam("id") Long id) {
        ServiceWelcomeConfigDO welcomeConfig = welcomeConfigService.getWelcomeConfig(id);
        return success(BeanUtils.toBean(welcomeConfig, ServiceWelcomeConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得欢迎语配置分页")
    @PreAuthorize("@ss.hasPermission('service:welcome-config:query')")
    public CommonResult<PageResult<ServiceWelcomeConfigRespVO>> getWelcomeConfigPage(@Valid ServiceWelcomeConfigPageReqVO pageReqVO) {
        PageResult<ServiceWelcomeConfigDO> pageResult = welcomeConfigService.getWelcomeConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServiceWelcomeConfigRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得欢迎语配置列表")
    @PreAuthorize("@ss.hasPermission('service:welcome-config:query')")
    public CommonResult<List<ServiceWelcomeConfigRespVO>> getWelcomeConfigList() {
        ServiceWelcomeConfigPageReqVO pageReqVO = new ServiceWelcomeConfigPageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceWelcomeConfigDO> pageResult = welcomeConfigService.getWelcomeConfigPage(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, ServiceWelcomeConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出欢迎语配置 Excel")
    @PreAuthorize("@ss.hasPermission('service:welcome-config:export')")
    @OperateLog(type = EXPORT)
    public void exportWelcomeConfigExcel(@Valid ServiceWelcomeConfigPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceWelcomeConfigDO> list = welcomeConfigService.getWelcomeConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "欢迎语配置.xls", "数据", ServiceWelcomeConfigRespVO.class,
                BeanUtils.toBean(list, ServiceWelcomeConfigRespVO.class));
    }

}